home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7513 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: what happens w/delete called twice ?
  5. Date: Fri, 23 Feb 1996 15:58:43 GMT
  6. Organization: Netcom
  7. Message-ID: <312dcc35.138429140@nntp.ix.netcom.com>
  8. References: <kcc.423.0EE12CF6@interaccess.com>
  9. NNTP-Posting-Host: ix-dc11-06.ix.netcom.com
  10. X-NETCOM-Date: Fri Feb 23  7:58:29 AM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. kcc@interaccess.com (kcc) wrote:
  14.  
  15. > Hello,
  16. >     Can someone tell me... what happens when delete is called twice for the same 
  17. > memory deallocation ie.,
  18. >     SOME_STRUCT    *struct_p    = new SOME STRUCT;
  19. >     if ( foo_a() )
  20. >     {
  21. >         delete struct_p;
  22. >         retvalue = 0;
  23. >     }
  24. >     else
  25. >     {
  26. >         retvalue = foo_b();
  27. >     }
  28. >     delete struct_p;
  29. >     return( retvalue );
  30.  
  31. According to the draft this results in undefined behavior.
  32.  
  33. In practice it is very unlikely that anything good will happen.  With
  34. many (most) compilers you will corrupt the heap resulting in bizarre
  35. behavior later in the program.
  36.  
  37. Don't do it.
  38.  
  39. Note that delete of the null pointer is always legal and harmless so
  40. you can protect yourself in situations where it is not convenient to
  41. ensure only a single delete is done simply by assigning 0 to the
  42. pointer variable.
  43.  
  44. Michael M Rubenstein
  45.